package cn.edu.scau.cmi.chenyipeng.composite.consistent;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.chenyipeng.composite.safe.SafeComponent;
import cn.edu.scau.cmi.chenyipeng.composite.safe.SafeComposite;
import cn.edu.scau.cmi.chenyipeng.composite.safe.SafeLeaf;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class ConsistentCompositeController {

	private TreeItem <ConsistentComponent> rootItem;
	private ConsistentComponent root;
	
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField nameTF;

    @FXML
    private TreeView<ConsistentComponent> treeView;

    @FXML
    private Button addTeam;

    @FXML
    private Button createRootButton;

    @FXML
    private Button addPerson;

    @FXML
    private Button delete;

    @FXML
    void createRoot(ActionEvent event) {
    	root = new ConsistentComposite();
    	((ConsistentComposite)root).setName(nameTF.getText());
    	rootItem = new TreeItem(root);
    	rootItem.setExpanded(true);
    	showTreeView();
    }

    @FXML
    void addTeamChild(ActionEvent event) {
    	TreeItem chosenItem = treeView.getSelectionModel().getSelectedItem();
    	if(chosenItem.getValue() instanceof ConsistentComposite && nameTF.getText().compareTo("")!=0) {
    		//判断被插入节点是否为团队节点且输入团队名不能为空
    		ConsistentComponent tempTeam = new ConsistentComposite();
    		((ConsistentComposite)tempTeam).setName(nameTF.getText());
    		((ConsistentComposite)tempTeam).setParent(((ConsistentComponent)chosenItem.getValue()));
    		TreeItem tempTeamItem = new TreeItem(tempTeam);
    		tempTeamItem.setExpanded(true);
    		((ConsistentComposite)chosenItem.getValue()).addChild(tempTeam);
    		chosenItem.getChildren().add(tempTeamItem);
    	}
    	else {
    		//被插入节点不为团队或团队名为空就不能插入
    	}
    	showTreeView();
    }

    @FXML
    void addPersonChild(ActionEvent event) {
    	TreeItem chosenItem = treeView.getSelectionModel().getSelectedItem();
    	if(chosenItem.getValue() instanceof ConsistentComposite && nameTF.getText().compareTo("")!=0) {
    		ConsistentComponent tempPerson = new ConsistentLeaf();
    		((ConsistentLeaf)tempPerson).setName(nameTF.getText());
    		((ConsistentLeaf)tempPerson).setParent(((ConsistentComponent)chosenItem.getValue()));
    		TreeItem tempPersonItem = new TreeItem(tempPerson);
    		((ConsistentComposite)chosenItem.getValue()).addChild(tempPerson);
    		chosenItem.getChildren().add(tempPersonItem);
    	}
    	else {
    		
    	}
    	showTreeView();
    }

    @FXML
    void deleteChild(ActionEvent event) {
    	TreeItem chosenItem = treeView.getSelectionModel().getSelectedItem();
    	TreeItem chosenParentItem = chosenItem.getParent();
    	if(chosenItem.getValue() instanceof ConsistentLeaf) {
    		ConsistentComponent tempParent = ((ConsistentLeaf)(chosenItem.getValue())).getParent();
        	((ConsistentComposite)tempParent).deleteChild(((ConsistentLeaf)chosenItem.getValue()));
        	chosenParentItem.getChildren().remove(chosenItem);
    	}
    	else {
    		ConsistentComponent tempParent = ((ConsistentComposite)(chosenItem.getValue())).getParent();
    		if(tempParent != null) {
    			((ConsistentComposite)tempParent).deleteChild(((ConsistentComposite)chosenItem.getValue()));
    			chosenParentItem.getChildren().remove(chosenItem);
    		}
    		else{
    			rootItem = null;
    		}
    	}
    	showTreeView();
    }

    @FXML
    void showTreeView() {
    	treeView.setRoot(rootItem);
    }
    
    @FXML
    void initialize() {
        assert nameTF != null : "fx:id=\"nameTF\" was not injected: check your FXML file 'ConsistentCompositeUI.fxml'.";
        assert treeView != null : "fx:id=\"treeView\" was not injected: check your FXML file 'ConsistentCompositeUI.fxml'.";
        assert addTeam != null : "fx:id=\"addTeam\" was not injected: check your FXML file 'ConsistentCompositeUI.fxml'.";
        assert createRootButton != null : "fx:id=\"createRootButton\" was not injected: check your FXML file 'ConsistentCompositeUI.fxml'.";
        assert addPerson != null : "fx:id=\"addPerson\" was not injected: check your FXML file 'ConsistentCompositeUI.fxml'.";
        assert delete != null : "fx:id=\"delete\" was not injected: check your FXML file 'ConsistentCompositeUI.fxml'.";

    }
}

